package vs.tech.auto.imu;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class GUI extends Activity 
{
	///
	///	The local instance of IMU, a thread that listens to android hardware sensors
	///		to tell us really cool stuff.
	///
	private IMU mIMU;
	
	///
	///	The local instance of SensorManager. Its purpose should be obvious.
	///
	private SensorManager mSensorManager;
	
	///
	///	TextView handles
	///
	private DisplayHandles mDisplayHandles = new DisplayHandles();
	
	///
	///	Called upon application creation, very similar to constructor.
	///
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //	After setting up generic application info (above) lets initialize all variables.
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        
        //	Populate the text views
        mDisplayHandles.accel_X = (TextView) findViewById(R.id.Accel_X);
        mDisplayHandles.accel_Y = (TextView) findViewById(R.id.Accel_Y);
        mDisplayHandles.accel_Z = (TextView) findViewById(R.id.Accel_Z);
        mDisplayHandles.vel_X = (TextView) findViewById(R.id.Vel_X);
        mDisplayHandles.vel_Y = (TextView) findViewById(R.id.Vel_Y);
        mDisplayHandles.vel_Z = (TextView) findViewById(R.id.Vel_Z);
        mDisplayHandles.dpos_X = (TextView) findViewById(R.id.DeltaPos_X);
        mDisplayHandles.dpos_Y = (TextView) findViewById(R.id.DeltaPos_Y);
        mDisplayHandles.dpos_Z = (TextView) findViewById(R.id.DeltaPos_Z);
        mDisplayHandles.averageExec = (TextView) findViewById(R.id.AverageExec);
        mDisplayHandles.buffersize = (TextView) findViewById(R.id.BufferSize);
        mDisplayHandles.pitch = (TextView) findViewById(R.id.Pitch);
        mDisplayHandles.roll = (TextView) findViewById(R.id.Roll);
        mDisplayHandles.yaw = (TextView) findViewById(R.id.Yaw);
        
        //	And initialize the IMU instance
        mIMU = new IMU(this, mDisplayHandles);
        
    }
    
  	///
	///	Override the onResume function for setting up the sensors so that we get them all back when
    ///		the application must flush memeory.
	///
    @Override
    public void onResume()
    {
    	super.onResume();
    	///
    	///	Register several sensors with the IMU
    	///
    	if ( mSensorManager != null )
    	{
    		mSensorManager.registerListener( 	mIMU,
												mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
												SensorManager.SENSOR_DELAY_FASTEST);

    		mSensorManager.registerListener(	mIMU,
												mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
												SensorManager.SENSOR_DELAY_FASTEST);
    	}
    	
    	//	After all of that jazz, start the thread.
    	mIMU.start();
    }
    
    ///
	///	Override onStop to free resources appropriately
	///    
    @Override
    protected void onStop() 
    {
        mSensorManager.unregisterListener(mIMU);
        super.onStop();
    }
    
    
      
    
    
    
}